﻿namespace HuaWei
{
    using System;
    using System.Drawing;
    using System.IO;

    public class ImageCommon
    {
        public static Image GetImageFromLocalDir(string filename)
        {
            try
            {
                string path = Path.Combine(IniFile.ReadIniFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "System.ini"), "ImagePath", "Path", false), filename);
                if (!Directory.Exists(path))
                {
                    return null;
                }
                return new Bitmap(path);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static Image GetImageFromStr(string str)
        {
            MemoryStream stream = new MemoryStream(Convert.FromBase64String(str));
            try
            {
                return new Bitmap(stream);
            }
            catch (Exception)
            {
            }
            finally
            {
                stream.Close();
            }
            return null;
        }

        public static string GetStrFromImage(Image i)
        {
            MemoryStream stream = new MemoryStream();
            try
            {
                i.Save(stream, i.RawFormat);
                return Convert.ToBase64String(stream.ToArray());
            }
            catch (Exception)
            {
            }
            finally
            {
                stream.Close();
            }
            return "";
        }
    }
}

